﻿using System;
using System.Linq;
using CSUtils;
using G02.PlayerData;
using U3dUtils;
using UnityEngine;

namespace TripleJump {
    public class CRoleAi {
        TripleJump_FristRoleFly role;

        public void Init(TripleJump_FristRoleFly role) {
            this.role = role;
            if (TripleJump_FristRoleFly.CPara.firstRoleId == role.id) {
                return;
            }
            CMonoMonitor.UpdateEventAdd("dddd", 0, "ddd", Update);
        }

        float clickStartTime;
        bool isRaning;

        Part_021_BloodStrip bloodStrip;

        public void GameReset() {
            if (TripleJump_FristRoleFly.CPara.firstRoleId == role.id) {
                return;
            }

            if (bloodStrip == null) {
                bloodStrip = role.bloodStrip;
            }
            bloodStrip.SetColor(CColor.GetColor(UnityEngine.Random.Range(0, 256)));
            var name = CString.GetUserName();
            bloodStrip.SetName(name, CColor.GetColor(UnityEngine.Random.Range(0, 256)));
        }

        float startJumpLine;

        public void S_GameStart() {
            if (TripleJump_FristRoleFly.CPara.firstRoleId == role.id) {
                return;
            }
            startJumpLine = TripleJump_FristRoleFly.CPara.startJumpLine + (UnityEngine.Random.value - 0.5f) * 2;
            if (UnityEngine.Random.value < 0.3f) {
                startJumpLine -= UnityEngine.Random.value * 6;
            }

            isRaning = true;
        }

        void Update(float ff) {
            if (isRaning == false) {
                return;
            }
            if (role.flyCount == 0) {
                if (role.position.z > startJumpLine) {
                    role.ClickFly();
                }
            }
            if (role._flyState == TripleJump_FristRoleFly.EFlyState.e06_downLand) {
                if (clickStartTime < Time.time) {
                    float value = UnityEngine.Random.value;
                    if (value > 0.03f) {
                        clickStartTime = Time.time + UnityEngine.Random.value * 0.2f;
                    } else if (value > 0.01f) {
                        clickStartTime = Time.time + UnityEngine.Random.value * 0.6f;
                    } else {
                        clickStartTime = Time.time + UnityEngine.Random.value * 2;
                    }
                    role.ClickFly();
                }
            }
        }

        public void GameEnd() {
            if (TripleJump_FristRoleFly.CPara.firstRoleId == role.id) {
                return;
            }
            isRaning = false;
        }
    }
}
